package com.example.music.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.music.R;
import com.example.music.adapter.PlaylistAdapter;
import com.example.music.model.Song;

import java.io.Serializable;
import java.util.List;

public class PlaylistDialogFragment extends DialogFragment {

    private List<Song> songList;
    private PlaylistAdapter adapter;
    private OnSongSelectedListener listener;

    public interface OnSongSelectedListener {
        void onSongSelected(int position);
    }

    public static PlaylistDialogFragment newInstance(List<Song> songs, int currentPosition) {
        PlaylistDialogFragment fragment = new PlaylistDialogFragment();
        Bundle args = new Bundle();
        args.putSerializable("songs", (Serializable) songs);
        args.putInt("current_position", currentPosition);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, R.style.FullscreenDialog);
        if (getArguments() != null) {
            songList = (List<Song>) getArguments().getSerializable("songs");
        }
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof OnSongSelectedListener) {
            listener = (OnSongSelectedListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnSongSelectedListener");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_playlist, container, false);

        RecyclerView recyclerView = view.findViewById(R.id.recycler_view_playlist);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));

        adapter = new PlaylistAdapter(songList, position -> {
            if (listener != null) {
                listener.onSongSelected(position);
                dismiss(); // 关闭弹窗
            }
        });

        // ✅ 获取当前播放位置
        int currentPosition = -1;
        if (getArguments() != null) {
            currentPosition = getArguments().getInt("current_position", -1);
        }

        // ✅ 设置高亮
        if (currentPosition >= 0) {
            adapter.setCurrentPosition(currentPosition);
        }


        recyclerView.setAdapter(adapter);

        Button btnClose = view.findViewById(R.id.btn_close_playlist);
        btnClose.setOnClickListener(v -> dismiss());

        return view;
    }
    @Override
    public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = (int) (getResources().getDisplayMetrics().widthPixels * 0.9); // 占 90%
            params.height=(int)(getResources().getDisplayMetrics().heightPixels*0.6);
            window.setAttributes(params);
            window.setBackgroundDrawableResource(R.drawable.bg_dialog); // 自定义背景
        }
    }
}